package server

import (
	"context"
	"fmt"
	"math/rand"
	"srv-server/basic/global"
	__ "srv-server/basic/proto"
	"srv-server/handler/model"
	"time"
)

type Server struct {
	__.UnimplementedSrvServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) Login(_ context.Context, in *__.LoginRequest) (*__.LoginResponse, error) {
	var u model.User

	global.DB.Where("mobile = ?", in.Mobile).Find(&u)
	fmt.Println(u)
	if u.Id == 0 {
		return &__.LoginResponse{
			Type: false,
			Msg:  "账号不存在",
		}, nil
	}
	if u.Password != in.Password {
		return &__.LoginResponse{
			Type: false,
			Msg:  "密码错误",
		}, nil
	}

	return &__.LoginResponse{
		Id:   int32(u.Id),
		Msg:  "登录成功",
		Type: true,
	}, nil
}
func (s *Server) ListPack(_ context.Context, in *__.ListPackRequest) (*__.ListPackResponse, error) {

	var pack []model.Pack

	global.DB.Limit(1).Find(model.Pack{})
	fmt.Println(model.Pack{})
	var list []*__.GetPack
	for _, packages := range pack {
		getPack := __.GetPack{
			FormUserId: 1,
			PackagesId: "1",
			Weight:     float32(packages.Weight),
			Address:    packages.Address,
			Urgent:     packages.Urgent,
		}
		list = append(list, &getPack)
	}

	return &__.ListPackResponse{
		List: list,
	}, nil
}
func (s *Server) WeightPack(_ context.Context, in *__.WeightPackRequest) (*__.WeightPackResponse, error) {

	var pack []model.Pack

	global.DB.Find(&model.Pack{})

	var list []*__.WeightPack
	for _, packages := range pack {
		getPack := __.WeightPack{
			Weight: float32(packages.Weight),
		}
		list = append(list, &getPack)
	}

	return &__.WeightPackResponse{
		List: list,
	}, nil
}
func (s *Server) WeightSortPack(_ context.Context, in *__.WeightSortPackRequest) (*__.WeightSortPackResponse, error) {

	var pack []model.Pack

	global.DB.Order("created_at desc").Find(&model.Pack{})

	var list []*__.SortPack
	for _, packages := range pack {
		getPack := __.SortPack{
			PackagesId: packages.PackagesId,
			Weight:     float32(packages.Weight),
			Address:    packages.Address,
			Urgent:     packages.Urgent,
		}
		list = append(list, &getPack)
	}

	return &__.WeightSortPackResponse{
		List: list,
	}, nil
}
func (s *Server) PutSms(_ context.Context, in *__.PutSmsRequest) (*__.PutSmsResponse, error) {
	code := rand.Intn(90000) + 10000

	_, err := global.Rdb.Set(context.Background(), "PutSms"+in.PackagesId+in.Source, code, time.Minute*10).Result()
	if err != nil {
		return &__.PutSmsResponse{
			Type: false,
		}, nil
	}

	return &__.PutSmsResponse{
		Type: true,
	}, nil
}
func (s *Server) PutPack(_ context.Context, in *__.PutPackRequest) (*__.PutPackResponse, error) {
	var pack model.Pack
	global.DB.Where("packages_id = ?", in.PackagesId).Find(pack)
	if pack.Id == 0 {

		return &__.PutPackResponse{
			Msg:  "包裹不存在",
			Type: false,
		}, nil
	}
	result, _ := global.Rdb.Get(context.Background(), "PutSms"+in.PackagesId+"PutPack").Result()
	if result != in.PutSms {
		return &__.PutPackResponse{
			Msg:  "取件码错误",
			Type: false,
		}, nil
	}
	return &__.PutPackResponse{
		Msg:  "取件成功",
		Type: true,
	}, nil
}
